package com.edu.controller.Slider;

import com.alibaba.fastjson2.JSONObject;
import com.edu.dto.ResultDto;
import com.edu.entity.SliderEntity;
import com.edu.service.SliderService;
import com.edu.service.impl.SliderServiceImpl;
import com.edu.utils.ReturnCode;
import com.edu.utils.ServletHelper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * com.edu.controller.Silder
 * 2024/7/5 10:01
 *
 * @author Fancy
 * @version 1.0
 */
@WebServlet(urlPatterns = {"/sliders/*","/sliders"})
public class SliderController extends HttpServlet {
    SliderService sliderService = new SliderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");

        List<SliderEntity> sliderEntities = sliderService.getAllSlider();

        if (sliderEntities != null) {
            String rst = ResultDto.success(sliderEntities);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(rst);
        } else {
            String rst = ResultDto.fail(ReturnCode.INTERNAL_SERVER_ERROR.getCode(), "内部错误");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(rst);
        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        int sliderId = Integer.parseInt(req.getPathInfo().substring(1));

        JSONObject json = ServletHelper.getRequestBodyAsJson(req);
        String newStatus = json.getString("status");

        //从请求头获取token
        String token = req.getHeader("token");

        int row = sliderService.updateSliderStatus(newStatus,sliderId,token);

        if (row >= 1) {
            String success = ResultDto.success(null);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(success);
        } else if (row == 0) {
            String fail = ResultDto.fail(ReturnCode.UNAUTHORIZED.getCode(), "更新失败,权限不足");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        } else {
            String fail = ResultDto.fail(ReturnCode.INTERNAL_SERVER_ERROR.getCode(), "内部错误");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        }
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        int sliderId = Integer.parseInt(req.getPathInfo().substring(1));

        //从请求头获取token
        String token = req.getHeader("token");
        int row = sliderService.deleteSlider(sliderId,token);

        if (row >= 1) {
            String success = ResultDto.success(null);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(success);
        } else if (row == 0) {
            String fail = ResultDto.fail(ReturnCode.UNAUTHORIZED.getCode(), "删除失败,权限不足");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        } else {
            String fail = ResultDto.fail(ReturnCode.INTERNAL_SERVER_ERROR.getCode(), "内部错误");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");

        SliderEntity slider = ServletHelper.getRequestBodyAsObject(req, SliderEntity.class);

        //从请求头获取token
        String token = req.getHeader("token");
        int row = sliderService.createSlider(slider,token);


        if (row >= 1) {
            String success = ResultDto.success(null);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(success);
        } else if (row == 0) {
            String fail = ResultDto.fail(ReturnCode.UNAUTHORIZED.getCode(), "添加失败,权限不足");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        } else {
            String fail = ResultDto.fail(ReturnCode.INTERNAL_SERVER_ERROR.getCode(), "内部错误");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        }
    }
}
